package com.ruoyi.factorydata.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.factorydata.domain.dto.HomePlanedProductionDTO;
import com.ruoyi.factorydata.domain.dto.HomeTopRankProjectDTO;
import com.ruoyi.factorydata.domain.dto.HomeWorkOrderLineDTO;
import com.ruoyi.factorydata.mapper.HomeStatisticsMapper;
import com.ruoyi.factorydata.service.IHomeStatisticsService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class HomeStatisticsServiceImpl implements IHomeStatisticsService {


    private final HomeStatisticsMapper homeStatisticsMapper;

    public HomeStatisticsServiceImpl(HomeStatisticsMapper homeStatisticsMapper) {
        this.homeStatisticsMapper = homeStatisticsMapper;
    }

    @Override
    public HomePlanedProductionDTO getHomePlanedProduction(HomePlanedProductionDTO homePlanedProductionDTO) {
        /**
         * 两部走，第一步查询条件传入的时间段中的工单计划数量
         */
        if(homePlanedProductionDTO == null){
            homePlanedProductionDTO = new HomePlanedProductionDTO();
        }
        Long homeProjectNumber = homeStatisticsMapper.getHomeProjectNumber(homePlanedProductionDTO);
        homePlanedProductionDTO.setProjectNumber(homeProjectNumber);
        Long homeProcessProjectNumber = homeStatisticsMapper.getHomeProcessProjectNumber(homePlanedProductionDTO);
        homePlanedProductionDTO.setProgressProjectNumber(homeProcessProjectNumber);
        Long homePlanedProductionNumber = homeStatisticsMapper.getHomePlanedProductionNumber(homePlanedProductionDTO);
        homePlanedProductionDTO.setPlannedProductionNumber(homePlanedProductionNumber);
        Long homeComPlatedProductionNumber = homeStatisticsMapper.getHomeComPlatedProductionNumber(homePlanedProductionDTO);
        homePlanedProductionDTO.setComPlatedProductionNumber(homeComPlatedProductionNumber);
        Long homeAllWorkOrderNumber = homeStatisticsMapper.getHomeAllWorkOrderNumber(homePlanedProductionDTO);
        homePlanedProductionDTO.setAllWorkOrderNumber(homeAllWorkOrderNumber);
        Long homeUnProcessWorkOrderNumber = homeStatisticsMapper.getHomeUnProcessWorkOrderNumber(homePlanedProductionDTO);
        homePlanedProductionDTO.setUnProcessWorkOrderNumber(homeUnProcessWorkOrderNumber);
        Long homeProcessWorkOrderNumber = homeStatisticsMapper.getHomeProcessWorkOrderNumber(homePlanedProductionDTO);
        homePlanedProductionDTO.setProcessWorkOrderNumber(homeProcessWorkOrderNumber);
        Long homeComPlatedWorkOrderNumber = homeStatisticsMapper.getHomeGetComPlatedWorkOrderNumber(homePlanedProductionDTO);
        homePlanedProductionDTO.setComPlatedWorkOrderNumber(homeComPlatedWorkOrderNumber);
        return homePlanedProductionDTO;

    }

    @Override
    public HomeWorkOrderLineDTO getHomeWorkOrderLineData(HomeWorkOrderLineDTO homeWorkOrderLineDTO) {
        if(homeWorkOrderLineDTO == null){
            homeWorkOrderLineDTO = new HomeWorkOrderLineDTO();
        }
        if (homeWorkOrderLineDTO.getStartTime() == null){
            homeWorkOrderLineDTO.setStartTime(new Date(new Date().getTime() - 1000 * 60 * 60 * 24 * 15));
        }
        if (homeWorkOrderLineDTO.getEndTime() == null){
            homeWorkOrderLineDTO.setEndTime(new Date());
        }
        String[] daysBetween = DateUtils.getDaysBetween(homeWorkOrderLineDTO.getStartTime(), homeWorkOrderLineDTO.getEndTime());
        List<HomeWorkOrderLineDTO.WorkOrderPlanedProduction> workOrderPlanedProductions =new ArrayList<>();
        List<HomeWorkOrderLineDTO.WorkOrderComPlatedProduction> workOrderComPlatedProductions =new ArrayList<>();
        Map<String, HomeWorkOrderLineDTO.WorkOrderPlanedProduction> planMap = homeStatisticsMapper.getWorkOrderPlanedProductionNumber(homeWorkOrderLineDTO)
                .stream()
                .collect(Collectors.toMap(HomeWorkOrderLineDTO.WorkOrderPlanedProduction::getPlanDate, sta -> sta, (it1, ite2) -> it1));
        Map<String, HomeWorkOrderLineDTO.WorkOrderComPlatedProduction> workOrderMap = homeStatisticsMapper.getWorkOrderComPlatedProductionNumber(homeWorkOrderLineDTO)
                .stream()
                .collect(Collectors.toMap(HomeWorkOrderLineDTO.WorkOrderComPlatedProduction::getActualDate, sta -> sta, (it1, ite2) -> it1));
        for (String day : daysBetween) {
            HomeWorkOrderLineDTO.WorkOrderPlanedProduction workOrderPlanedProduction = planMap.get(day);
            if (workOrderPlanedProduction == null){
                workOrderPlanedProduction = new HomeWorkOrderLineDTO.WorkOrderPlanedProduction();
                workOrderPlanedProduction.setPlanDate(day);
                workOrderPlanedProduction.setWorkOrderPlanedProductionNumber(0L);
            }
            workOrderPlanedProductions.add(workOrderPlanedProduction);
            HomeWorkOrderLineDTO.WorkOrderComPlatedProduction workOrderComPlatedProduction = workOrderMap.get(day);
            if (workOrderComPlatedProduction == null){
                workOrderComPlatedProduction = new HomeWorkOrderLineDTO.WorkOrderComPlatedProduction();
                workOrderComPlatedProduction.setActualDate(day);
                workOrderComPlatedProduction.setWorkOrderComPlatedProductionNumber(0L);
            }
            workOrderComPlatedProductions.add(workOrderComPlatedProduction);
        }
        homeWorkOrderLineDTO.setWorkOrderPlanedProduction(workOrderPlanedProductions);
        homeWorkOrderLineDTO.setWorkOrderComPlatedProduction(workOrderComPlatedProductions);
        return homeWorkOrderLineDTO;
    }

    @Override
    public List<HomeTopRankProjectDTO> getHomeTopRankProject(HomeTopRankProjectDTO homeTopRankProjectDTO) {
        if(homeTopRankProjectDTO == null){
            homeTopRankProjectDTO = new HomeTopRankProjectDTO();
        }
        return homeStatisticsMapper.getHomeTopRankProject(homeTopRankProjectDTO);
    }
}
